package com.mbc.web.common.client.ui;

import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.Scheduler;
import com.google.gwt.core.client.Scheduler.ScheduledCommand;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyDownEvent;
import com.google.gwt.event.dom.client.KeyDownHandler;
import com.google.gwt.event.logical.shared.CloseEvent;
import com.google.gwt.event.logical.shared.CloseHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.mbc.common.entities.BranchType;
import com.mbc.common.entities.BranchTypeRpt;
import com.mbc.web.common.client.ClientSingleton;
import com.mbc.web.common.client.CommonControllerMgr;
import com.mbc.web.common.client.CommonMessage;
import com.mbc.web.common.client.ViewUtils;
import com.mbc.web.common.client.ui.Modal;
import com.mbc.web.common.client.ui.control.MButton;
import com.mbc.web.common.client.ui.control.MTextBox;
import com.mbc.web.common.shared.HttpStatus;

public class FrmBranchTypeRpt extends Composite {

	/* MTextBox */
	@UiField
	TextBox txtfrombranchTypeID;
	@UiField
	MTextBox txttobranchTypeID;

	/* MButtons */
	@UiField
	MButton btnPreview;
	@UiField
	MButton btnClear;
	@UiField MButton btnBack;
	@UiField
	Button btnfromSearch;
	@UiField
	Button btntoSearch;

	BranchType branchType;
	BranchTypeRpt branchTypeRpt;
	byte action_type;

	private static FrmBranchTypeRptUiBinder uiBinder = GWT
			.create(FrmBranchTypeRptUiBinder.class);

	interface FrmBranchTypeRptUiBinder extends
			UiBinder<Widget, FrmBranchTypeRpt> {
	}

	public FrmBranchTypeRpt() {
		initWidget(uiBinder.createAndBindUi(this));
		initialize();
	}

	private void initialize() {
		
		txtfrombranchTypeID.getElement().setAttribute("style", "text-transform: uppercase;");
		txttobranchTypeID.getElement().setAttribute("style", "text-transform: uppercase;");
		
		Scheduler.get().scheduleDeferred(new ScheduledCommand() {

			@Override
			public void execute() {
				txtfrombranchTypeID.setFocus(true);
			}
		});
		txttobranchTypeID.addKeyDownHandler(new KeyDownHandler() {

			@Override
			public void onKeyDown(KeyDownEvent event) {
				if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
					event.preventDefault();
					TextBox l_txt = (TextBox) event.getSource();
					if (l_txt == txttobranchTypeID)
						btnPreview.setFocus(true);
				}
			}
		});
	}

	@UiHandler("btnPreview")
	void onBtnPreviewClick(ClickEvent event) {
		ViewUtils.closeMessageBox();
		ViewUtils.startAjaxLoader(CommonMessage.loading_message);
		CommonControllerMgr.prepareBranchTypeReport(bindBranchType(),
				BranchTypeReportCallback());
	}

	private BranchTypeRpt bindBranchType() {
		BranchTypeRpt l_entity = new BranchTypeRpt();
		l_entity.setFromBranchTypeID(txtfrombranchTypeID.getText());
		l_entity.setToBranchTypeID(txttobranchTypeID.getText());
		return l_entity;
	}

	private AsyncCallback<Integer> BranchTypeReportCallback() {
		return new AsyncCallback<Integer>() {

			@Override
			public void onSuccess(Integer result) {
				ViewUtils.stopAjaxLoader();
				if (result == HttpStatus.OK) {
					ClientSingleton.viewReport("Branch Type Report");
				} else if (result == HttpStatus.NOT_FOUND) {
					ViewUtils.showErrorMessage("No records.");
				}
			}

			@Override
			public void onFailure(Throwable caught) {

			}
		};
	}

	@UiHandler("btnfromSearch")
	void onbtnfromSearchClick(ClickEvent event) {
		final SearchBranchType l_dia = new SearchBranchType("Branch Type List");

		l_dia.showDialog(new CloseHandler<Modal>() {

			public void onClose(CloseEvent<Modal> event) {
				if (l_dia.getSelectedEntity() != null) {
					branchType = l_dia.getSelectedEntity();
					txtfrombranchTypeID.setText(branchType.getBranchTypeCode());
					txttobranchTypeID.setFocus(true);
				} else
					txtfrombranchTypeID.setFocus(true);
			}
		});
	}

	@UiHandler("btntoSearch")
	void onbtntoSearchClick(ClickEvent event) {
		final SearchBranchType l_dia = new SearchBranchType("Branch Type List");

		l_dia.showDialog(new CloseHandler<Modal>() {

			public void onClose(CloseEvent<Modal> event) {
				if (l_dia.getSelectedEntity() != null) {
					branchType = l_dia.getSelectedEntity();
					txttobranchTypeID.setText(branchType.getBranchTypeCode());
					txtfrombranchTypeID.setFocus(true);
				} else
					txttobranchTypeID.setFocus(true);
			}
		});
	}

	@UiHandler("btnClear")
	void onBtnClearClick(ClickEvent event) {
		txtfrombranchTypeID.setText("");
		txttobranchTypeID.setText("");
		txtfrombranchTypeID.setFocus(true);
	}

	@UiHandler("btnBack")
	void onBtnBackClick(ClickEvent event) {
		ClientSingleton.changeView(new FrmBranchType());
	}

	@UiHandler("txtfrombranchTypeID")
	void ontxtfrombranchTypeIDClick(KeyDownEvent event) {
		if (event.getNativeKeyCode() == 121) { // F10
			onbtnfromSearchClick(null);
		}
	}

	@UiHandler("txttobranchTypeID")
	void ontxttobranchTypeIDClick(KeyDownEvent event) {
		if (event.getNativeKeyCode() == 121) { // F10
			onbtntoSearchClick(null);
		}
	}
	@UiHandler("txtfrombranchTypeID")
	void ontxtfrombranchTypeIDChange(ChangeEvent event) {

		if (!txtfrombranchTypeID.getText().equals("")){
			BranchType l_entity = new BranchType();
			l_entity.setBranchTypeCode(txtfrombranchTypeID.getText());
			ViewUtils.startAjaxLoader(CommonMessage.loading_message);
			CommonControllerMgr.findBranchTypeByCode(l_entity ,  frombranchTypeIDCallback());
					
		}
	}
	private AsyncCallback<BranchType> frombranchTypeIDCallback() {
		return new AsyncCallback<BranchType>() {

			@Override
			public void onSuccess(BranchType result) {
				ViewUtils.stopAjaxLoader();
				if (result != null) {
					ViewUtils.closeMessageBox();	
					txtfrombranchTypeID.setText(result.getBranchTypeCode());
					txtfrombranchTypeID.getElement().setAttribute("id", String.valueOf(result.getBranchTypeId()));
				} else {
					ViewUtils
							.showErrorMessage("BranchType Code does not exist.");
					txtfrombranchTypeID.setText("");
					txtfrombranchTypeID.setFocus(true);
				}
			}
			@Override
			public void onFailure(Throwable caught) {
			}
		};
	}
	@UiHandler("txttobranchTypeID")
	void ontxttobranchTypeIDChange(ChangeEvent event) {

		if (!txttobranchTypeID.getText().equals("")){
			BranchType l_entity = new BranchType();
			l_entity.setBranchTypeCode(txttobranchTypeID.getText());
			ViewUtils.startAjaxLoader(CommonMessage.loading_message);
			CommonControllerMgr.findBranchTypeByCode(l_entity,  tobranchTypeIDCallback());
					
		}
	}
	private AsyncCallback<BranchType> tobranchTypeIDCallback() {
		return new AsyncCallback<BranchType>() {

			@Override
			public void onSuccess(BranchType result) {
				ViewUtils.stopAjaxLoader();
				if (result != null) {
					ViewUtils.closeMessageBox();	
					txttobranchTypeID.setText(result.getBranchTypeCode());
					txttobranchTypeID.getElement().setAttribute("id", String.valueOf(result.getBranchTypeId()));
				} else {
					ViewUtils
							.showErrorMessage("BranchType Code does not exist.");
					txttobranchTypeID.setText("");
					txttobranchTypeID.setFocus(true);
				}
			}
			@Override
			public void onFailure(Throwable caught) {
			}
		};
	}
}
